package cn.tedu.fruitshop.controller;

import cn.tedu.fruitshop.pojo.dto.CouponsAddNewDTO;
import cn.tedu.fruitshop.pojo.vo.CouponsListItemVO;
import cn.tedu.fruitshop.service.CouponsService;
import cn.tedu.fruitshop.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author www
 * @create 2022-11-05 17:52
 */
@Slf4j
@Valid
@Api(tags = "06.优惠券管理模块")
@RestController
@RequestMapping("/coupons")
public class CouponsController {

    @Autowired
    CouponsService couponsService;

    //http:localhost:8080/coupons/add-new
    @ApiOperation("添加优惠券")
    @ApiOperationSupport(order = 101)
    @PostMapping("/add-new")
    public JsonResult insert(CouponsAddNewDTO couponsAddNewDTO) {
        log.debug("开始处理【添加优惠券的请求】，参数：{}",couponsAddNewDTO);
        couponsService.add(couponsAddNewDTO);
        return JsonResult.ok();
    }

    //http:localhost:8080/coupons/couponName/delete
    @ApiOperation("根据优惠券名称删除优惠券")
    @ApiOperationSupport(order = 201)
    @ApiImplicitParam(name = "couponName", value = "优惠券名称", required = true, dataType = "String")
    @PostMapping("/{couponName}/delete")
    public JsonResult<Void> deleteByTitle(@NotNull(message = "优惠券名称不能为空") @PathVariable String couponName) {
        log.debug("开始处理【根据优惠券名称删除优惠券】的请求，参数：{}", couponName);
        couponsService.deleteByCouponName(couponName);
        return JsonResult.ok();
    }

    // http://localhost:8080/coupons
    @ApiOperation("查询优惠券列表")
    @ApiOperationSupport(order = 401)
    @GetMapping("")
    public JsonResult<List<CouponsListItemVO>> list() {
        log.debug("开始处理【查询优惠券列表】的请求，无参数");
        List<CouponsListItemVO> list = couponsService.list();
        return JsonResult.ok(list);
    }

}
